package com.h3bpm.web.protalHanding;

import OThinker.H3.Controller.ControllerBase;
import com.alibaba.fastjson.JSON;
import com.h3bpm.handler.JSONUtils;
import com.h3bpm.web.common.utils.HttpLocalUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.util.Map;

public class ThreadPost extends ControllerBase {

    private static Logger logger = LoggerFactory.getLogger(ThreadPost.class);
    /**
     *
     * 发送数据  protal 使用
     *
     * 当发送失败时根据配置次数重试
     * @param
     * @param
     * @param paramMap 请求内容
     * @return 返回 null/"" 时成功,否则返回失败内容
     */
    public static Map<String, Object> postRequestResult(String postUrl, String paramMap, String authorization){
        String url = null;
        int i = 0;
        while (true){
            String requestString=null;
            logger.debug("发送前——————————————————-"+postUrl+"___"+paramMap);
             requestString = HttpLocalUtils.doPost2(postUrl,paramMap,"");
            logger.debug("发送后——————————————————-"+requestString);
            // 发送后返回信息
            try {
                if(StringUtils.isNotBlank(requestString)){
                    Map<String, Object> stringObjectMap = JSONUtils.parseMap(requestString);
                    if( "".equals(stringObjectMap.get("code"))||stringObjectMap.get("code")==null){
                        Thread.sleep(1000);
                    } else {
                        return  stringObjectMap;
                    }
                }
            } catch (Exception e){
                System.out.println(e.getMessage());
            }
            i ++;
            if(i > 3) {
                break;
            }
            // 大于尝试次数退出
        }
        return null;
    }

    @Override
    public String getFunctionCode() {
        return null;
    }
}
